<?php
namespace app\index\controller;
use think\Controller;
use \GatewayWorker\Lib\Gateway;

class Index extends Controller
{
    public function index()
    {
    	return $this->fetch();
    }

    //
    public function test()
    {
    	$client_id = '7f00000108fc00000c28';//
    	$room_id = 1;
    	$ddddd = Gateway::leaveGroup($client_id,$room_id);

    	$ddddd = Gateway::closeClient($client_id);
    	$new_message = array('action'=>'logout', 'from_client_id'=>$client_id);
           Gateway::sendToGroup($room_id, json_encode($new_message));


    	$pk_id = input('pk_id',100);
    	$score_total = input('score_total',0);//当前用户总分数
    	$to_user_score_total = input('to_user_score_total',0);//对方用户总分数
    	$answer_data = json_decode(input('answer'),true);
    	//胜负 1胜，0平，2负
    	if($score_total>$to_user_score_total){
    		$is_win = 1;
    	}else if($score_total==$to_user_score_total){
    		$is_win = 2;
    	}else{
    		$is_win = 3;
    	}

    	$answer_data = array(
	    	array(
	    		"question_id"=>10,
	    		"answer_id"=>3,
	    		"answer_val"=>0,
	    		"answer_seconds"=>2
	    		),
	    	array(
	    		"question_id"=>11,
	    		"answer_id"=>2,
	    		"answer_val"=>0,
	    		"answer_seconds"=>1
	    		)
	    	);

    	$data = db('user_subject_pk')->where(['id'=>$pk_id])->find();
    	if(!empty($data)){
    		$map['id'] = ['in',$data['questions_id']];
    		//查找出对应问题
    		$questions_info = db('questions')->where($map)->select();

    		//用户答案
    		$new_answer_data = [];
    		foreach((array)$answer_data as $key => $row){
    			$new_answer_data[$row['question_id']] = $row;
    		}
    		foreach((array)$questions_info as $key => $row){
    			$answer = unserialize($row['answer']);
    			foreach($answer as $key => $v) {
    				if(!empty($new_answer_data[$row['id']]) && $new_answer_data[$row['id']]['question_id']==$row['id'] && $new_answer_data[$row['id']]['answer_val']){//正确的

    				}else{//错误的

    				}
    			}
    		}

    		//var_dump($questions_info);exit;
    	}

    	//var_dump(json_decode($_POST['data'],1));exit;

    	//奖励处理 
    	//判断是否使用加金币，加经验道具
    	$add_point = 0;
    	$add_exp = 0;
    	if($is_win==1){//胜
    		$add_point = 20;
    		$add_exp = 3;
    	}else if($is_win==2){//平
    		$add_point = 0;
    		$add_exp = 2;
    	}else if($is_win==3){//负
    		$add_point = 0;
    		$add_exp = 1;
    	}


    	$data = [
    		'is_win'=>$is_win,
    		'add_point'=>$add_point,
    		'add_exp'=>$add_exp,
    		'ddd'=>$ddddd
    	];


    	$this->json_return(1,'成功',$data);

    }

    public function json_return($status, $message = '', $data = array(),$pages=1){
        $return = array();
        $return['status'] = $status;
        $return['message'] = $message;
        $return['data'] = $data;
        if($pages>1)$return['pages'] = $pages;

        echo json_encode($return);
        exit;
    }


//解密微信数据
function decrypt(){

    \think\Loader::import('org.wxBizMsgCrypt',EXTEND_PATH);
    $wxapp_config = config('wechat.wxapp');//支付配置
    $appid = $wxapp_config['appid'];
    $sessionKey = 'MonKXtkQm4+7h37RfD4/Cg==';
    $iv = "pN2WbvBWA1j3Szc4CmW+zg==";
    $encryptedData="9taoAo3nBKaxVgSqNbvtMKfiNqeGvDiWSQobqnHGSbUGdRs3z8xlATjXxLsbnDf59oyCbF+hFIh9LHI1DOtUYDA0y7V9z6xyMaJ85GNR4jjQyMjai37LaHGgHrhAa5lbpj4soFTCTkokJ5uaWF9qWw==";
    
    $pc = new \WXBizDataCrypt($appid, $sessionKey);
    $errCode = $pc->decryptData($encryptedData, $iv, $data );

    if ($errCode == 0) {
        print($data . "\n");
    } else {
        print($errCode . "\n");
    }


}





}
